package com.xq.config;

import com.xq.handler.AdminLoginHandlerInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/*
 * @Author 徐柯
 * @Description 可以使用自定义类扩展MVC的功能
 * @Date 20:11 2021/4/18
 * @Param 
 * @return 
 **/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {


    @Value("${file.staticAccessPath}")
    private String staticAccessPath;
    @Value("${file.uploadFolder}")
    private String uploadFolder;



    @Bean
    public AdminLoginHandlerInterceptor getAdminLoginHandlerInterceptor() {
        return new AdminLoginHandlerInterceptor();
    }

    /*
     * @Author 徐柯
     * @Description 静态资源拦截处理
     * @Date 20:11 2021/4/18
     * @Param [registry]
     * @return void
     **/
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(staticAccessPath).addResourceLocations("file:" + uploadFolder);
    }

    /**
     * @Author 徐柯
     * @Description 拦截器注册
     * @Date 20:11 2021/4/18
     * @Param [registry]
     * @return void
     **/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 3：后台拦截处理
        registry.addInterceptor(getAdminLoginHandlerInterceptor())
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/admin/login/**")
                .excludePathPatterns("/admin/logined")
                .excludePathPatterns("/admin/assert/**")
                .excludePathPatterns("/admin/logout")
                .addPathPatterns("/admin/**");
    }

}